package com.tianhao.luo.helper;

import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;

/**
 * CyclicBarrier测试
 *
 * @author tianhao.luo@hand-china.com
 */
public class CyclicBarrierDemo {
    public static final int NUMBER =7;
    // 模拟七龙珠召唤神龙
    public static void main(String[] args) {
        CyclicBarrier cyclicBarrier = new CyclicBarrier(NUMBER,()->{
            System.out.println("许愿神龙");
        });

        for (int i = 0; i < NUMBER; i++) {
            new Thread(()->{
                System.out.println(Thread.currentThread().getName()+" 星龙珠被收集了");

                try {
                    cyclicBarrier.await();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } catch (BrokenBarrierException e) {
                    e.printStackTrace();
                }
            },String.valueOf(i+1)).start();
        }

        System.out.println("hello world");
    }
}
